/*
 * <p>Title: DataGenerator.java </p>
 * <p>Copyright:  Copyright (C) 2002 - 2008 GuangDong Poson Techonlogy Co. Ltd</p>
 * <p>Company: Poson</p>
 * @author huang weijian
 * @version 1.0
 * @time: Apr 28, 2009
 */

package alvin.hwang.asyntree;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * @author huang weijian
 */
public class DataGenerator {

	private static final int SIZE = 5;
	private static final int CHILDREN_SIZE1 = 1;
	private static final int CHILDREN_SIZE2 = 3;
	
	public static void writeXml(String parentId) throws IOException {
		System.out.println("--------------------starting---------------------");
		File file = new File("WebRoot/tinytree/tree.xml");
		Writer out = new FileWriter(file);
		out.write(generateXml(parentId));
		out.flush();
		out.close();
		System.out.println("--------------------done---------------------");
	}
	
	public static String generateXml(String parentId) {
		StringBuffer xml = new StringBuffer();
		xml.append("<?xml version=\"1.0\" encoding=\"GBK\"?>\n");
		xml.append("<tree parentId=\"").append(parentId).append("\">\n");
		for (int i = 0; i < SIZE; i++) {
			xml.append("\t<node>\n");
			xml.append("\t\t<id>").append(parentId).append(i).append("</id>\n");
			xml.append("\t\t<name>Node").append(parentId).append(i).append("</name>\n");
			xml.append("\t\t<leaf>").append(i % 2 == 0).append("</leaf>\n");
			xml.append("\t\t\t\t<inputType>radio</inputType>\n");
			xml.append("\t\t\t\t<inputName>child1</inputName>\n");
			xml.append(generateChildren(parentId + i));
			xml.append("\t</node>\n");
		}
		xml.append("</tree>");
		return xml.toString();
	}
	
	private static String generateChildren(String parentId) {
		StringBuffer xml = new StringBuffer();
		xml.append("\t\t<children>\n");
		for (int i = 0; i < CHILDREN_SIZE1; i++) {
			xml.append("\t\t\t<node>\n");
			xml.append("\t\t\t\t<id>").append(parentId).append(i).append("</id>\n");
			xml.append("\t\t\t\t<name>Node").append(parentId).append(i).append("</name>\n");
			xml.append("\t\t\t\t<leaf>").append(i % 2 == 0).append("</leaf>\n");
			xml.append("\t\t\t\t<inputType>radio</inputType>\n");
			xml.append("\t\t\t\t<inputName>child2</inputName>\n");
			xml.append(generateChildren2(parentId + i));
			xml.append("\t\t\t</node>\n");
		}
		xml.append("\t\t</children>\n");
		return xml.toString();
	}
	
	private static String generateChildren2(String parentId) {
		StringBuffer xml = new StringBuffer();
		xml.append("\t\t<children>\n");
		for (int i = 0; i < CHILDREN_SIZE2; i++) {
			xml.append("\t\t\t<node>\n");
			xml.append("\t\t\t\t<id>").append(parentId).append(i).append("</id>\n");
			xml.append("\t\t\t\t<name>Node").append(parentId).append(i).append("</name>\n");
			xml.append("\t\t\t\t<leaf>true</leaf>\n");
			xml.append("\t\t\t\t<inputType>checkbox</inputType>\n");
			xml.append("\t\t\t\t<inputName>child3</inputName>\n");
			xml.append("\t\t\t</node>\n");
		}
		xml.append("\t\t</children>\n");
		return xml.toString();
	}
	
	public static void main(String[] args) throws IOException {
		writeXml("0");
	}
}
